/*
 * Copyright (c) 2025 iSOFT INFRASTRUCTURE SOFTWARE CO., LTD.
 * easyAda is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
#ifndef IPI_H
#define IPI_H

#include <core/task.h>
#include <core/handler.h>

#define IPI_SYNC_TIMEOUT 1000UL    ///< Timeout duration of asynchronous sending ms

/**
 * @brief   IPI的同步类型
 *
 */
enum ipi_mode {
    IPI_ASYNC_MODE = 1, /* send ack before ipi call is complete */
    IPI_SYNC_MODE,      /* wait untill the ipi call is complete */
};

enum ipi_data_state {
    IPIDATA_TASK_IN     = 0,
    IPIDATA_TASK_OUT    = 1,
    IPIDATA_TASK_KILL   = 2,
    IPIDATA_CALL_FUNC   = 3,
    IPIDATA_TASK_IPC    = 4,
    IPIDATA_SIGNAL_SEND = 5,
    IPIDATA_NOT_SET     = 6,
};

typedef struct ipi_data_t {
    uint32_t       i_sync_mode;
    uint8_t        i_type1;    // 1 级 type (必要)
    uint8_t        i_type2;    // 2 级 type (不必要)
    unsigned long  i_data1;
    unsigned long  i_data2;
    spinlock_t     lock;
    unsigned short state;
    unsigned int   target_cpu;
    tcb_t         *task;
    handler_t      handler;
    void          *msg;
} ipi_data_t;

extern ipi_data_t ipi_datas[];

void    ipi_setup_handler(void);
int32_t send_ipi_all_mode(uint32_t action, uint32_t cpu, ipi_data_t *data, uint64_t timeout_ms);

#endif
